package ac.svn.accesscontrol.ui.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

import ac.svn.accesscontrol.store.ISVNItem;
import ac.svn.accesscontrol.store.SVNGroup;

public class GroupListModel implements ListModel {

    List<SVNGroup> items;

    public GroupListModel()
    {
	items = new ArrayList<SVNGroup>(5);
    }


    @Override
    public SVNGroup getElementAt(int index) {
	if(index < 0 || index >= items.size())
	{
	    return null;
	}
	return items.get(index);
    }

    @Override
    public int getSize() {
	return items.size();
    }

    public void addGroup(ISVNItem item)
    {
	SVNGroup group = (SVNGroup) item;
	items.add(group);
    }

    public void addGroups(Set<ISVNItem> usersList)
    {
	for(ISVNItem item : usersList)
	{
	    SVNGroup group = (SVNGroup) item;
	    items.add(group);
	}
	sortList();
    }

    public void clearList()
    {
	items.clear();
    }

    public void sortList()
    {
	Collections.sort(items);
    }

    public int indexOf(String userName)
    {
	for(ISVNItem item : items)
	{
	    SVNGroup group = (SVNGroup) item;
	    if(group.getName().toUpperCase().contains(userName.toUpperCase()))
	    {
		return items.indexOf(group);
	    }
	}
	return 0;
    }

    @Override
    public void addListDataListener(ListDataListener l) {
	// TODO Auto-generated method stub

    }
    @Override
    public void removeListDataListener(ListDataListener l) {
	// TODO Auto-generated method stub

    }

}
